<template>
	<div class="container">
		<el-dialog
			title="成品入库"
			:visible.sync="showDialog_copy"
			class="main_dialog dialog400"
			@closed="closeOrderDialog"
		>
			<el-form
				:inline="false"
				:model="productInfo"
				class="demo-form-inline"
				ref="craftInfoRef"
				label-width="100px"
				key="userListFrom1"
			>
				<el-form-item label="成品名称" prop="name">
					<el-input
						v-model="productInfo.name"
						disabled
						placeholder="成品名称"
						class="wid220"
					></el-input>
				</el-form-item>
				<el-form-item label="加工数量" prop="process_num">
					<el-input
						v-model="productInfo.process_num"
						disabled
						placeholder="加工数量"
						class="wid220"
					></el-input>
				</el-form-item>
				<el-form-item label="已加工数量" prop="processed_num">
					<el-input
						v-model="productInfo.processed_num"
						disabled
						placeholder="加工数量"
						class="wid220"
					></el-input>
				</el-form-item>
				<el-form-item label="未加工数量" prop="processing_num">
					<el-input
						v-model="productInfo.processing_num"
						disabled
						placeholder="加工数量"
						class="wid220"
					></el-input>
				</el-form-item>
				<el-form-item label="成本" prop="cost">
					<el-input-number
						v-model="productInfo.cost"
						placeholder="成本"
						class="wid220"
						:controls="false"
						:min="0"
						:precision="2"
					></el-input-number>
				</el-form-item>
				<el-form-item label="本次入库数量" prop="num">
					<el-input-number
						v-model="productInfo.num"
						placeholder="入库数量"
						class="wid220"
						:controls="false"
						:min="0"
						:precision="2"
					></el-input-number>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button
					type="primary"
					:loading="saveloading"
					@click="preserve(1)"
					v-repeatClick
					>部分入库</el-button
				>
				<el-button
					type="primary"
					:loading="saveloading"
					@click="preserve(2)"
					v-repeatClick
					>完全入库</el-button
				>
				<el-button @click="showDialog_copy = false">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<script>
	import { process_storage } from "@/api/sorting/processOrder";
	export default {
		name: "",
		props: {
			showDialog: {
				type: Boolean,
				default: false,
			},
			form: {
				type: Object,
				default: function () {
					return {};
				},
			},
		},
		computed: {
			showDialog_copy: {
				get: function () {
					return this.showDialog;
				},
				set: function (newval) {
					this.$emit("update:showDialog", newval);
				},
			},
		},
		watch: {
			form: {
				handler(val, oldval) {
					this.productInfo = JSON.parse(JSON.stringify(this.form));
				},
				deep: true,
			},
		},
		data() {
			return {
				listLoading: false,
				saveloading: false,
				productInfo: {},
			};
		},
		methods: {
			preserve(type) {
				this.saveloading = true;
				this.productInfo.status = type;
				let processing_num = parseFloat(this.productInfo.processing_num);
				let num = parseFloat(this.productInfo.num);
				let message = "";
				switch (type) {
					case 1:
						if (num <= processing_num) {
							message = "确认入库？";
						} else {
							message = "入库数量已超过加工数量，是否完成入库？";
						}
						break;
					case 2:
						if (num >= processing_num) {
							message = "确认完成入库？";
						} else {
							message = "入库数量小于加工数量，确认完成入库？";
						}
						break;
				}
				this.$confirm(message, "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
				})
					.then(() => {
						this.process_storage();
					})
					.catch(() => {
						this.saveloading = false;
					});
			},
			process_storage() {
				let data = {
					id: this.productInfo.id,
					num: this.productInfo.num,
					cost: this.productInfo.cost,
					status: this.productInfo.status,
				};
				process_storage(data).then((res) => {
					if (res.errcode == 0) {
						this.$message.success("成品入库成功");
						this.showDialog_copy = false;
						this.$emit("refresh");
					} else {
						this.$message.error(res.errmsg);
					}
					this.saveloading = false;
				});
			},
			closeOrderDialog() {
				// this.productInfo = {};
				this.saveloading = false;
			},
			// 商品选择框清除
			clearGoodsInput(type) {
				switch (type) {
					case 1:
						// this.craftInfo.form.goods_spu_id = "";
						// this.craftInfo.form.goods_id = "";
						break;
					case 2:
						break;
				}
			},
			// 商品选择框选择
			getGoodsInputData(data, type) {
				// console.log(data);
				switch (type) {
					case 1:
						// this.craftInfo.form.goods_id = data.goods_id;
						// this.craftInfo.form.goods_spu_id = data.id;
						// this.$nextTick(() => {
						//   this.$refs["craftInfoRef"].clearValidate("goods_spu_id");
						// });
						break;
					case 2:
						// this.craftInfo.add_form.goods_id = data.goods_id;
						// this.craftInfo.add_form.goods_spu_id = data.id;
						// this.craftInfo.add_form.spu_unit = data.spu_unit.title;
						// let spu_title = data.spu_name ? data.spu_name.title : "";
						// this.craftInfo.add_form.name = `${data.goods.name}【${spu_title}】`;
						// this.$nextTick(() => {
						//   this.$refs["addGoodsNum"].focus();
						// });
						break;
				}
			},
		},
	};
</script>
<style lang='scss' scoped>
.add_goods_container {
	display: flex;
	align-items: center;
	padding: 0 0 0 0px;
	li {
		display: flex;
		align-items: center;
		span {
			padding-right: 10px;
			display: block;
			width: 60px;
			text-align: right;
			em {
				padding: 0 5px;
				color: red;
			}
		}
		.width40 {
			width: 60px;
		}
		&:last-child {
			margin-left: 10px;
		}
	}
}
>>> .table_container .el-table--small td .cell {
	height: auto;
}
.icon {
	margin-left: 5px;
	vertical-align: baseline;
}
/deep/ .el-input-number .el-input__inner {
	text-align: left;
}
</style>

